package com.zenith.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.edu.mapper.EduMajorMapper;
import com.zenith.edu.po.domain.EduMajor;
import com.zenith.edu.po.dto.EduMajorDTO;
import com.zenith.edu.po.vo.EduMajorVO;
import com.zenith.edu.service.EduMajorService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EduMajorServiceImpl extends ServiceImpl<EduMajorMapper, EduMajor> implements EduMajorService {
    
    @Override
    public EduMajorVO createMajor(EduMajorDTO dto) {
        EduMajor major = new EduMajor();
        BeanUtils.copyProperties(dto, major);
        this.save(major);
        
        EduMajorVO vo = new EduMajorVO();
        BeanUtils.copyProperties(major, vo);
        return vo;
    }
    
    @Override
    public EduMajorVO getMajorById(Long id) {
        EduMajor major = this.getById(id);
        if (major == null) {
            return null;
        }
        
        EduMajorVO vo = new EduMajorVO();
        BeanUtils.copyProperties(major, vo);
        return vo;
    }
    
    @Override
    public EduMajorVO updateMajor(EduMajorDTO dto) {
        EduMajor major = new EduMajor();
        BeanUtils.copyProperties(dto, major);
        this.updateById(major);
        
        EduMajorVO vo = new EduMajorVO();
        BeanUtils.copyProperties(major, vo);
        return vo;
    }
    
    @Override
    public boolean deleteMajor(Long id) {
        return this.removeById(id);
    }
    
    @Override
    public List<EduMajorVO> getMajorList() {
        List<EduMajor> majorList = this.list();
        return majorList.stream().map(major -> {
            EduMajorVO vo = new EduMajorVO();
            BeanUtils.copyProperties(major, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}